<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/22
 * Time: 09:52
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use ttcommon\base\Arrays;
use yii\db\Migration;

/**
 * @property integer $id
 * @property integer $type
 * @property string $keyword
 * @property string $content
 */
class WeContent extends TtActiveRecord
{
    public static function tableName()
    {
        return 'we_content';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'id' => $migration->primaryKey()->notNull(),
            'title' => $migration->string(20)->notNull()->defaultValue('')->comment($this->getAttributeLabel('title')),
            'type' => $migration->integer(1)->notNull()->defaultValue(self::TYPE_TEXT)->comment($this->getAttributeLabel('type')),
            'keyword' => $migration->string(30)->notNull()->defaultValue('')->comment($this->getAttributeLabel('keyword')),
            'content' => $migration->text()
        ]);
        $migration->createIndex('keyword', $tableName, 'keyword');
    }

    const TYPE_TEXT = 0;
    const TYPE_TPL = 1;

    public static function typeText($type = null)
    {
        static $text = [
            self::TYPE_TEXT => '普通文本',
            self::TYPE_TPL => '模板文本(TODO)'
        ];
        return Arrays::constText($text, $type);
    }

    public function attributeLabels()
    {
        return [
            'id' => '#',
            'title' => '标题',
            'type' => '类型',
            'keyword' => '关键词',
            'content' => '内容'
        ];
    }

    public function rules()
    {
        return [
            ['type', 'integer'],
            ['title', 'string', 'length' => [1, 20]],
        ];
    }

    public function scenarios()
    {
        return [
            self::SCENARIO_DEFAULT => ['title', 'type', 'keyword', 'content']
        ];
    }
}